home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1996 #15 / Monster Media Number 15 (Monster Media)(July 1996).ISO / internet / jdraw.zip / JDRAW.EXE / data.z / Blink.bsp next >
Text File  |  1996-05-27  |  2KB  |  69 lines

  1. /*
  2.  * Copyright (c) 1994 Sun Microsystems, Inc. All Rights Reserved.
  3.  */
  4.  
  5. import java.awt.*;
  6. import java.util.StringTokenizer;
  7.  
  8. /**
  9.  * I love blinking things.
  10.  *
  11.  * @author Arthur van Hoff
  12.  */
  13. public class blink extends java.applet.Applet implements Runnable {
  14.     Thread blinker;
  15.     String lbl;
  16.     Font font;
  17.     int speed;
  18.  
  19.     public void init() {
  20.     font = new java.awt.Font("TimesRoman", Font.PLAIN, 24);
  21.     String att = getParameter("speed");
  22.     speed = (att == null) ? 400 : (1000 / Integer.valueOf(att).intValue());
  23.     att = getParameter("lbl");
  24.     lbl = (att == null) ? "Blink" : att;
  25.     }
  26.     
  27.     public void paint(Graphics g) {
  28.     int x = 0, y = font.getSize(), space;
  29.     int red = (int)(Math.random() * 50);
  30.     int green = (int)(Math.random() * 50);
  31.     int blue = (int)(Math.random() * 256);
  32.     Dimension d = size();
  33.  
  34.     g.setColor(Color.black);
  35.     g.setFont(font);
  36.     FontMetrics fm = g.getFontMetrics();
  37.     space = fm.stringWidth(" ");
  38.     for (StringTokenizer t = new StringTokenizer(lbl) ; t.hasMoreTokens() ; ) {
  39.         String word = t.nextToken();
  40.         int w = fm.stringWidth(word) + space;
  41.         if (x + w > d.width) {
  42.         x = 0;
  43.         y += font.getSize();
  44.         }
  45.         if (Math.random() < 0.5) {
  46.         g.setColor(new java.awt.Color((red + y * 30) % 256, (green + x / 3) % 256, blue));
  47.         } else {
  48.         g.setColor(Color.lightGray);
  49.         }
  50.         g.drawString(word, x, y);
  51.         x += w;
  52.     }
  53.     }
  54.  
  55.     public void start() {
  56.     blinker = new Thread(this);
  57.     blinker.start();
  58.     }
  59.     public void stop() {
  60.     blinker.stop();
  61.     }
  62.     public void run() {
  63.     while (true) {
  64.     try {Thread.currentThread().sleep(speed);} catch (InterruptedException e){}
  65.         repaint();
  66.     }
  67.     }
  68. }
  69.